package cn.com.surker.interceptor;

import cn.com.surker.redis.RedisUtils;
import cn.com.surker.system.cache.LogCache;
import cn.com.surker.system.entity.SysLog;
import cn.com.surker.system.model.CacheUser;
import cn.com.surker.util.IpUtils;
import cn.com.surker.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Map;

/**
 * @ClassName UserMessage
 * @Description 用户消息拦截器,用于拦截点击的链接操作，并根据用户订阅的类型，发送到相关用户信息中
 * @Author luotao
 * @Date 2021/9/14 10:48
 * @Version 1.0
 */
@Slf4j
@Component
public class ErpLogInterceptor implements HandlerInterceptor {

    @Resource
    protected RedisUtils redisUtils;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		SysLog sysLog = new SysLog();
		//获取用户操作信息，存入缓存中
		// url
		String requestURI = request.getRequestURI();
		// ip
		String ipAddress = IpUtils.getIpAddress(request);
		// 终端信息
		String device = request.getHeader("User-Agent");
		// 用户信息
		String access_token = "";
		HttpSession session = request.getSession();
		if (!StringUtils.isNull(session.getAttribute("oauth_token")))
			access_token = StringUtils.convertString(request.getSession().getAttribute("oauth_token"));
		if (!StringUtils.isNull(access_token)){
			if (redisUtils.exists(access_token)){
				CacheUser user = (CacheUser) redisUtils.get(access_token);
				Long id = user.getId();
				String username = user.getUsername();
				sysLog.setUid(id);
				sysLog.setUsername(username);
			}
		}
		// 请求参数
		Map<String, String[]> parameterMap = request.getParameterMap();
		String parmas = JSONObject.toJSONString(parameterMap);
		// 请求时间
		Date date = new Date();
		sysLog.setUrl(requestURI);
		sysLog.setIp(ipAddress);
		sysLog.setDevice(device);
		sysLog.setParams(parmas);
		sysLog.setCreateTime(date);
		//存入缓存中
		LogCache.getInstance().put(sysLog);
		return true;
	}

	/**
     * 在处理完成后进行操作
     * @param request
     * @param response
     * @throws Exception
     */
    /*@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        SysLog sysLog = new SysLog();
        //获取用户操作信息，存入缓存中
        // url
        String requestURI = request.getRequestURI();
        // ip
        String ipAddress = IpUtils.getIpAddress(request);
        // 终端信息
        String device = request.getHeader("User-Agent");
        // 用户信息
        String access_token = "";
		HttpSession session = request.getSession();
		if (!StringUtils.isNull(session.getAttribute("oauth_token")))
            access_token = StringUtils.convertString(request.getSession().getAttribute("oauth_token"));
        if (!StringUtils.isNull(access_token)){
            if (redisUtils.exists(access_token)){
                CacheUser user = (CacheUser) redisUtils.get(access_token);
                Long id = user.getId();
                String username = user.getUsername();
                sysLog.setUid(id);
                sysLog.setUsername(username);
            }
        }
        // 请求参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        String parmas = JSONObject.toJSONString(parameterMap);
        // 请求时间
        Date date = new Date();
        sysLog.setUrl(requestURI);
        sysLog.setIp(ipAddress);
        sysLog.setDevice(device);
        sysLog.setParams(parmas);
        sysLog.setCreateTime(date);
        //存入缓存中
        LogCache.getInstance().put(sysLog);

    }*/

}